package com.rongda.deploy.operation.deploy.service.impl;

import com.rongda.deploy.operation.deploy.pojo.ServerDeploy;
import com.rongda.deploy.operation.deploy.service.DeployService;
import com.rongda.deploy.operation.deploy.util.ExecuteShellUtil;
import org.springframework.stereotype.Service;

/**
 * Create By WeiBin on 2020/10/26 9:07
 *
 * @author WB
 */
@Service
public class DeployServiceImpl implements DeployService {
    @Override
    public boolean testConnect(ServerDeploy resources) {
        ExecuteShellUtil executeShellUtil = null;
        try {
            executeShellUtil = new ExecuteShellUtil(resources.getIp(), resources.getAccount(), resources.getPassword(), resources.getPort());
            if ("connect fail".equals(executeShellUtil.execute("ls"))) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            if (executeShellUtil != null) {
                executeShellUtil.close();
            }
        }
    }

    @Override
    public String executeCommend(String commend, ServerDeploy resources) {
        ExecuteShellUtil executeShellUtil = null;
        try {
            executeShellUtil = new ExecuteShellUtil(resources.getIp(), resources.getAccount(), resources.getPassword(), resources.getPort());
            return executeShellUtil.execute(commend);
        } catch (Exception e) {
            throw new RuntimeException("连接失败");
        } finally {
            if (executeShellUtil != null) {
                executeShellUtil.close();
            }
        }
    }
}
